%% User-defined columns in tables
% by Jaromir Benes
%
% In this m-file, we show how to create tables with user-defined columns
% using the `'columnstruct'` option. The option is an alternative to the
% `'range'` option, and allows more flexibility in defining the content of
% tables.

%% Clear Workspace

clear;
close all;
home;
irisrequired 8.20111202;

%% Create artificial data that will be reported

endhist = mm(2011,11);
range = mm(2000,1) : endhist;
n = 10;
d = struct();
for i = 1 : n
    name = sprintf('x%g',i);
    d.(name) = cumsum(tseries(range,@randn));
    d.(name) = d.(name) + 2*abs(min(d.(name)));
end

%% Conditional formats

cf = struct();
cf(1).test = 'col == 1 && value < -0.4';
cf(1).format = '\showdown{?}';

cf(2).test = 'col == 1 && value > 0.4';
cf(2).format = '\showup{?}';

cf(3).test = 'col == 1 && value >= -0.4 && value <= 0.4';
cf(3).format = '\showequal{?}';

%% User-defined structure of table columns
%
% We wish to create a table with the following 7 columns:
%
% # a symbol indicating an increase, decrease, or no change over the last
% month;
% # the value for the current month ;
% # the change over the last month;
% # the change over the last 6 months;
% # the change over the last 12 months;
% # the average over the last 6 months;
% # the average over the last 12 months.
%
% We create a 1-by-7 struct with three fields:
%
% * `'name'`, a column descriptor that will appear on the top in the
% headline;
%
% * `'func'`, a function that will be applied to the series to compute the
% relevant value for that particular column;
%
% * `'date'`, a date at which a value will be reported for the series;
% `'date'` does not need to be supplied if the function `'func'` evaluates
% itself to a numeric scalar.
%
% Note we can refer to the date `endhist` in the table column functions.

col = struct();

col.name = '';
col.func = @(x) x(endhist) - x(endhist-1);
col.date = [];

col(end+1).name = dat2char(endhist,'dateformat=','Mmm YYYY');
col(end).func = [];
col(end).date = endhist;

col(end+1).name = {'Change over the last','month'};
col(end).func = @(x) x(endhist) - x(endhist-1);
col(end).date = [];

col(end+1).name = {'Change over the last','6 months'};
col(end).func = @(x) x(endhist) - x(endhist-6);
col(end).date = [];

col(end+1).name = {'Change over the last','12 months'};
col(end).func = @(x) x(endhist) - x(endhist-12);
col(end).date = [];

col(end+1).name = {'Average over the last','6 months'};
col(end).func = @(x) mean(x(endhist-5:endhist));
col(end).date = [];

col(end+1).name = {'Average over the last','12 months'};
col(end).func = @(x)  mean(x(endhist-11:endhist));
col(end).date = [];

col %#ok<NOPTS>

%% Start new report

x = report.new();

% These are LaTeX commands to create the coloured arrows etc.
x.texcommand('\definecolor{darkgreen}{rgb}{0,0.6,0}');
x.texcommand('\newcommand{\showdown}[1]{\color{red}\blacktriangledown\hide{#1}}');
x.texcommand('\newcommand{\showup}[1]{\color{darkgreen}\blacktriangle\hide{#1}}');
x.texcommand('\newcommand{\showequal}[1]{\color{black}\thickapprox\hide{#1}}');


%% Create table with user-defined columns
%
% When using the option `'columnstruct'`, we do not need to specify the
% option `'range'` (and if we do it is ignored anyway). The table columns
% are entirealy defined by `'columnstruct'`.

x.table('Monthly monitoring report', ...
    'colstruct=',col,'condformat=',cf, ...
    'colwidth=',5,'arraystretch=',1.3,'tabcolsep=',1);

x.subheading('Financial market indicators');
x.series('Exchange rate',d.x1,'units=','XY$/US$');
x.series('Stock price index XY500',100*d.x2,'units=','p');
x.series('90-day interest rate',d.x2/2,'units=','% PA');

x.subheading('Real economic activity and prices');
x.series('CPI inflation',d.x6/3,'units=','% PA');
x.series('Industrial production',100*d.x7/3,'units=','Index');
x.series('Unemployment rate',d.x8,'units=','%');

x.subheading('Money and credit');
x.series('Credit to private sector',100*d.x6,'units=','bil XY');
x.series('Narrow money M0',100*d.x7,'units=','bil XY');
x.series('Broad money M3',200*d.x8,'units=','bil XY');

x %#ok<NOPTS>

%% Publish report to PDF file
%
% Compile the report into a PDF named `'report_user_latex_formats.pdf'`.
% The `open` command the PDF <?open?> may not work on some systems.

x.publish('report_table_with_user_columns.pdf','cleanup=',true);

open report_table_with_user_columns.pdf;

%% Help on IRIS functions used in this m-file
%
% Use either `help` to display help in the command window, or `idoc`
% to display help in an HTML browser window.
%
%    help report
%    help report/new
%    help report/table
%    help report/series
%    help report/subheading
%    help report/publish
%    help dates/mm
%    help tseries
%    help tseries/tseries
